import Vue from 'vue'
import VueRouter from 'vue-router'
import Profile from 'views/profile/Profile'
import NoteBox from 'views/notebox/NoteBox'
import ShopDetail from 'views/shop/ShopDetail'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const Login = () => import('views/Login')
const Home = () => import('views/Home')
const Dashboard = () => import('views/dashboard/Dashboard')
const ShopList = () => import('views/shop/ShopList')
// const ShopDetail = () => import('views/shop/ShopDetail')
const ShopMap = () => import('views/shop/ShopMap')
const BeerCatagory = () => import('views/beer/BeerCatagory')
const BeerList = () => import('views/beer/BeerList')
const Equipment = () => import('views/equipment/Equipment')
const UserList = () => import('views/userlist/UserList')
// const Profile = () => import('views/profile/Profile')
const SalesData = () => import('views/salesdata/SalesData')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    children: [
      {
        path: '',
        redirect: 'dashboard'
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard
      },
      {
        path: 'shoplist',
        name: 'ShopList',
        component: ShopList
      },
      {
        path: 'shopmap',
        name: 'ShopMap',
        component: ShopMap
      },
      {
        path: 'beerlist',
        name: 'BeerList',
        component: BeerList
      },
      {
        path: 'beercatagory',
        name: 'BeerCatagory',
        component: BeerCatagory
      },
      {
        path: 'salesdata',
        name: 'SalesData',
        component: SalesData
      },
      {
        path: 'equipment',
        name: 'Equipment',
        component: Equipment
      },
      {
        path: 'userlist',
        name: 'UserList',
        component: UserList
      },
      {
        path: 'profile',
        name: 'Profile',
        component: Profile
      },
      {
        path: 'notebox',
        name: 'NoteBox',
        component: NoteBox
      },
      {
        path: 'shopdetail',
        name: 'ShopDetail',
        component: ShopDetail
      }
    ]
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
